﻿Imports TBCC.UtilityMobile



Public Class StartUpDetailTypeService

    Private _pi As ProjectItem
    Private pid_str As String
    Private _sti As StartUpTypeBase

    Public Sub New(ByVal pi As ProjectItem, ByVal sti As StartUpTypeBase) 'ByVal sti As StartUpTypeItem
        _pi = pi
        If pi IsNot Nothing Then _
        pid_str = Trim(pi.ProjectID).Replace("-", "_")
        _sti = sti
    End Sub

    Public Function GetStartUpDetailGroupList(ByVal BeginTime As DateTime, ByVal EndTime As DateTime) As IList(Of StartUpDetailTypeItem)
        Dim il As IList(Of StartUpDetailTypeItem)
        il = New List(Of StartUpDetailTypeItem)

        'Dim dr As SQLite.SQLiteDataReader
        Dim dr As MySql.Data.MySqlClient.MySqlDataReader

        Dim strSql As String
        strSql = String.Format("SELECT * FROM TbccHDStartUpDetail_{0} ", pid_str)

        If BeginTime <> Nothing Then
            strSql &= String.Format("WHERE BeginTime>='{0}' ", UtilityClass.GetDateString(BeginTime))
            If EndTime <> Nothing Then
                strSql &= String.Format("AND EndTime<='{0}' ", UtilityClass.GetDateString(EndTime))
            End If
        End If

        dr = TBCC.UtilityMobile.SqlHelper.ExecuteDataReader(strSql, Nothing)

        If dr IsNot Nothing Then
            Dim seqNo As Integer = 1
            Do While dr.Read()
                Dim sdti As New StartUpDetailTypeItem()
                sdti.SeqNo = seqNo
                sdti.ID = dr.Item("ID")
                sdti.ParentID = dr.Item("ParentID")
                sdti.NetID = dr.Item("NetID")
                sdti.BeginTime = dr.Item("BeginTime")
                sdti.EndTime = dr.Item("EndTime")
                sdti.BeginAddress = dr.Item("BeginAddress").ToString()
                sdti.EndAddress = dr.Item("EndAddress").ToString()
                sdti.Shipper = dr.Item("Shipper").ToString()
                sdti.Carrier = dr.Item("Carrier").ToString()
                sdti.Receiver = dr.Item("Receiver").ToString()
                sdti.LastPageIndex = dr.Item("LastPageIndex")
                sdti.LastPageType = dr.Item("LastPageType")
                sdti.LastRecordTime = dr.Item("LastRecordTime")
                sdti.UploadStatus = dr.Item("UploadStatus")
                seqNo += 1
                il.Add(sdti)
            Loop
        End If

        Return il
    End Function

    Public Function GetStartUpDetailGroupList(ByVal ParentID As Integer) As IList(Of StartUpDetailTypeItem)

        Return GetStartUpDetailGroupList(Of StartUpDetailTypeItem)(ParentID)

    End Function

    Public Function GetStartUpDetailGroupList(Of T1 As {StartUpDetailTypeItem, New})(ByVal ParentID As Integer) As IList(Of T1)
        Dim il As IList(Of T1)
        il = New List(Of T1)

        Dim dr As MySql.Data.MySqlClient.MySqlDataReader

        Dim strSql As String
        strSql = String.Format("SELECT * FROM TbccHDStartUpDetail_{0} WHERE ParentID={1}", pid_str, ParentID)

        dr = TBCC.UtilityMobile.SqlHelper.ExecuteDataReader(strSql, Nothing)

        If dr IsNot Nothing Then
            Dim seqNo As Integer = 1
            Do While dr.Read()
                Dim sdti As New T1()
                sdti.SeqNo = seqNo
                sdti.ID = dr.Item("ID")
                sdti.ParentID = dr.Item("ParentID")
                sdti.NetID = dr.Item("NetID")
                sdti.BeginTime = dr.Item("BeginTime")
                sdti.EndTime = dr.Item("EndTime")
                sdti.BeginAddress = dr.Item("BeginAddress").ToString()
                sdti.EndAddress = dr.Item("EndAddress").ToString()
                sdti.Shipper = dr.Item("Shipper").ToString()
                sdti.Carrier = dr.Item("Carrier").ToString()
                sdti.Receiver = dr.Item("Receiver").ToString()
                sdti.LastPageIndex = dr.Item("LastPageIndex")
                sdti.LastPageType = dr.Item("LastPageType")
                sdti.LastRecordTime = dr.Item("LastRecordTime")
                sdti.UploadStatus = dr.Item("UploadStatus")
                seqNo += 1
                il.Add(sdti)
            Loop
        End If

        Return il
    End Function

    Public Function InsertStartUpDetailTypeItem(ByVal sdti As StartUpDetailTypeItem) As Boolean

        Dim strSql As String
        strSql = String.Format("INSERT INTO TbccHDStartUpDetail_{0}(NetID,ParentID,BeginTime,EndTime,BeginAddress,EndAddress,Shipper,Carrier,Receiver,UploadStatus,LastRecordTime,LastPageIndex,LastPageType) " _
                            & "VALUES(?p0,?p1,?p2,?p3,?p4,?p5,?p6,?p7,?p8,?p9,?p10,?p11,?p12)", pid_str)

        Dim p(12) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.Length - 1
            p(i) = New MySql.Data.MySqlClient.MySqlParameter()
            p(i).ParameterName = "p" + i.ToString
        Next

        p(0).Value = sdti.NetID
        p(1).Value = sdti.ParentID
        p(2).Value = sdti.BeginTime
        p(3).Value = sdti.EndTime
        p(4).Value = sdti.BeginAddress
        p(5).Value = sdti.EndAddress
        p(6).Value = sdti.Shipper
        p(7).Value = sdti.Carrier
        p(8).Value = sdti.Receiver
        p(9).Value = sdti.UploadStatus
        p(10).Value = sdti.LastRecordTime
        p(11).Value = sdti.LastPageIndex
        p(12).Value = sdti.LastPageType

        Return SqlHelper.ExecuteNonQuery(strSql, p)

    End Function

    Public Function UpdateStartUpDetailTypeItem(ByVal ID As Integer, ByVal UploadStatus As StartUploadStatus, ByVal LastRecordTime As DateTime, ByVal LastPageIndex As Integer, ByVal LastPageType As StartUploadPageType) As Boolean

        Dim strSql As String
        strSql = String.Format("UPDATE TbccHDStartUpDetail_{0} SET " _
                            & "UploadStatus=?p0,LastRecordTime=?p1,LastPageIndex=?p2,LastPageType=?p3 " _
                            & "WHERE ID=?p4 ", pid_str)

        Dim p(4) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.Length - 1
            p(i) = New MySql.Data.MySqlClient.MySqlParameter()
            p(i).ParameterName = "p" + i.ToString
        Next

        p(0).Value = UploadStatus
        p(1).Value = LastRecordTime
        p(2).Value = LastPageIndex
        p(3).Value = LastPageType
        p(4).Value = ID

        Return SqlHelper.ExecuteNonQuery(strSql, p)

    End Function

    Public Function UpdateStartUpDetailGroupList(Of T1 As {StartUpDetailTypeItem, New})(ByVal list As IList(Of T1)) As Boolean

        Dim tic As New TBCC.UtilityMobile.TranstrationItem()
        Dim strSql As String
        Dim index As Integer

        strSql = String.Format("DELETE FROM TbccHDStartUpDetail_{0} WHERE ParentID={1} -- " & index, pid_str, _sti.ID)
        tic.Add(strSql, Nothing)
        index += 1

        For Each sdti As StartUpDetailTypeItem In list
            strSql = String.Format("INSERT INTO TbccHDStartUpDetail_{0}(NetID,ParentID,BeginTime,EndTime,BeginAddress,EndAddress,Shipper,Carrier,Receiver,UploadStatus,LastRecordTime,LastPageIndex,LastPageType) " _
                                & "VALUES(?p0,?p1,?p2,?p3,?p4,?p5,?p6,?p7,?p8,?p9,?p10,?p11,?p12) -- " & index, pid_str)

            Dim p(12) As MySql.Data.MySqlClient.MySqlParameter
            For i As Integer = 0 To p.Length - 1
                p(i) = New MySql.Data.MySqlClient.MySqlParameter()
                p(i).ParameterName = "p" + i.ToString
            Next

            p(0).Value = sdti.NetID
            p(1).Value = _sti.ID 'sdti.ParentID
            p(2).Value = sdti.BeginTime
            p(3).Value = sdti.EndTime
            p(4).Value = sdti.BeginAddress
            p(5).Value = sdti.EndAddress
            p(6).Value = sdti.Shipper
            p(7).Value = sdti.Carrier
            p(8).Value = sdti.Receiver
            p(9).Value = sdti.UploadStatus
            p(10).Value = sdti.LastRecordTime
            p(11).Value = sdti.LastPageIndex
            p(12).Value = sdti.LastPageType

            tic.Add(strSql, p)
            index += 1
        Next

        Return SqlHelper.ExecuteTranstration(tic)
    End Function

End Class
